package cn.exrick.manager.dto;

import cn.exrick.common.pojo.ZTreeNode;
import cn.exrick.manager.dto.front.CartProduct;
import cn.exrick.manager.dto.front.Member;
import cn.exrick.manager.dto.front.Product;
import cn.exrick.manager.pojo.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;


public class DtoUtil{

    private final static Logger log= LoggerFactory.getLogger(DtoUtil.class);

    public static TbMember MemberDto2Member(MemberDto memberDto){

        TbMember tbMember =new TbMember();
        if(memberDto.getType() != null){
            tbMember.setType(memberDto.getType());
        }

        if(StringUtils.isNotBlank(memberDto.getUsername())){
            tbMember.setUsername(memberDto.getUsername());
        }

        if(StringUtils.isNotBlank(memberDto.getPassword())){
            tbMember.setPassword(memberDto.getPassword());
        }

        if(StringUtils.isNotBlank(memberDto.getPhone())){
            tbMember.setPhone(memberDto.getPhone());
        }

        if(StringUtils.isNotBlank(memberDto.getEmail())){
            tbMember.setEmail(memberDto.getEmail());
        }

        if(StringUtils.isNotBlank(memberDto.getSex())){
            tbMember.setSex(memberDto.getSex());
        }

        if(StringUtils.isNotBlank(memberDto.getDescription())){
            tbMember.setDescription(memberDto.getDescription());
        }

        if(memberDto.getOrganizationId() != null){
            tbMember.setOrganizationId(memberDto.getOrganizationId());
        }

        if(StringUtils.isNotBlank(memberDto.getMemberfile())){
            tbMember.setImage(memberDto.getMemberfile());
        }

        if(StringUtils.isNotBlank(memberDto.getProvince())){
            tbMember.setAddress(memberDto.getProvince()+" "
                    +memberDto.getCity()+" "+memberDto.getDistrict());
        }

        return tbMember;
    }

    public static TbItem ItemDto2TbItem(ItemDto itemDto){

        TbItem tbItem =new TbItem();

        tbItem.setTitle(itemDto.getTitle());
        tbItem.setPrice(itemDto.getPrice());
        tbItem.setCid(itemDto.getCid());
        tbItem.setImage(itemDto.getImage());
        tbItem.setIntroduction(itemDto.getIntroduction());
        tbItem.setNum(itemDto.getNum());
        tbItem.setTag(itemDto.getTag());
        tbItem.setPromotionPrice(itemDto.getPromotionPrice());
        tbItem.setSaleDate(itemDto.getSaleDate());
        tbItem.setPromotionDateFrom(itemDto.getPromotionDateFrom());
        tbItem.setPromotionDateTo(itemDto.getPromotionDateTo());
        tbItem.setStatus(itemDto.getStatus());
        tbItem.setRemindNum(itemDto.getRemindNum());
        tbItem.setCreateBy(itemDto.getCreateBy());
        tbItem.setUpdateBy(itemDto.getUpdateBy());
        return tbItem;
    }

    public static ItemDto TbItem2ItemDto(TbItem tbItem){

        ItemDto itemDto =new ItemDto();

        itemDto.setTitle(tbItem.getTitle());
        itemDto.setPrice(tbItem.getPrice());
        itemDto.setCid(tbItem.getCid());
        itemDto.setImage(tbItem.getImage());
        itemDto.setIntroduction(tbItem.getIntroduction());
        itemDto.setNum(tbItem.getNum());
        itemDto.setTag(tbItem.getTag());
        itemDto.setSaleDate(tbItem.getSaleDate());
        itemDto.setPromotionPrice(tbItem.getPromotionPrice());
        itemDto.setPromotionDateFrom(tbItem.getPromotionDateFrom());
        itemDto.setPromotionDateTo(tbItem.getPromotionDateTo());
        itemDto.setRemindNum(tbItem.getRemindNum());
        itemDto.setCreateBy(tbItem.getCreateBy());
        itemDto.setUpdateBy(tbItem.getUpdateBy());
        return itemDto;
    }



    public static ZTreeNode TbItemCat2ZTreeNode(TbItemCat tbItemCat){

        ZTreeNode zTreeNode =new ZTreeNode();

        zTreeNode.setId(Math.toIntExact(tbItemCat.getId()));
        zTreeNode.setStatus(tbItemCat.getStatus());
        zTreeNode.setSortOrder(tbItemCat.getSortOrder());
        zTreeNode.setName(tbItemCat.getName());
        zTreeNode.setpId(Math.toIntExact(tbItemCat.getParentId()));
        zTreeNode.setIsParent(tbItemCat.getIsParent());
        zTreeNode.setRemark(tbItemCat.getRemark());
        zTreeNode.setEnglishName(tbItemCat.getEnglishName());
        return zTreeNode;
    }

    public static TreeNode TbItemCat2TreeNode(TbItemCat tbItemCat){
        TreeNode treeNode = new TreeNode();
        treeNode.setText(tbItemCat.getName());
        treeNode.setTags(Collections.singletonList(String.valueOf(tbItemCat.getId())));
        treeNode.setState(new State());
        return treeNode;
    }

    public static ZTreeNode TbOrganization2ZTreeNode(TbOrganization tbOrganization){

        ZTreeNode zTreeNode =new ZTreeNode();

        zTreeNode.setId(Math.toIntExact(tbOrganization.getId()));
        zTreeNode.setSortOrder(tbOrganization.getSortOrder());
        zTreeNode.setName(tbOrganization.getName());
        zTreeNode.setpId(Math.toIntExact(tbOrganization.getParentId()));
        zTreeNode.setIsParent(tbOrganization.getIsParent());
        zTreeNode.setBalance(tbOrganization.getBalance());
        zTreeNode.setChargeUserId(tbOrganization.getChargeUserId());
        zTreeNode.setChargeUserName(tbOrganization.getChargeUserName());
        zTreeNode.setRemark(tbOrganization.getRemark());
        zTreeNode.setInitBalance(tbOrganization.getInitBalance());
        return zTreeNode;
    }

    public static Product TbItem2Product(TbItem tbItem){

        Product product =new Product();

        product.setProductId(tbItem.getId());
        product.setProductName(tbItem.getTitle());
        product.setSalePrice(tbItem.getPrice());
        product.setSubTitle(tbItem.getIntroduction());
        product.setProductImageBig(tbItem.getImages()[0]);
        product.setTag(tbItem.getTag());
        product.setNum(tbItem.getNum());
        return product;
    }

    public static Member TbMemer2Member(TbMember tbMemer){

        Member member =new Member();

        member.setId(tbMemer.getId());
        member.setUsername(tbMemer.getUsername());
        member.setEmail(tbMemer.getEmail());
        member.setPhone(tbMemer.getPhone());
        member.setAddress(tbMemer.getAddress());
        member.setImage(tbMemer.getImage());
        member.setOrganizationId(tbMemer.getOrganizationId());
        member.setStatus(tbMemer.getStatus());
        member.setSex(tbMemer.getSex());
        member.setDescription(tbMemer.getDescription());

        return member;
    }

    public static CartProduct TbItem2CartProduct(TbItem tbItem){

        CartProduct cartProduct =new CartProduct();

        cartProduct.setProductId(tbItem.getId());
        cartProduct.setProductName(tbItem.getTitle());
        cartProduct.setSalePrice(tbItem.getPrice());
        cartProduct.setProductImg(tbItem.getImages()[0]);
        cartProduct.setIntroduction(tbItem.getIntroduction());
        return cartProduct;
    }

    public static CartProduct TbOrderItem2CartProduct(TbOrderItem tbOrderItem){

        CartProduct cartProduct=new CartProduct();
        cartProduct.setProductId(Long.valueOf(tbOrderItem.getItemId()));
        cartProduct.setProductName(tbOrderItem.getTitle());
        cartProduct.setSalePrice(tbOrderItem.getPrice());
        cartProduct.setProductNum(Long.valueOf(tbOrderItem.getNum()));
        cartProduct.setProductImg(tbOrderItem.getPicPath());

        return cartProduct;
    }
}
